package leetcode.bst.p204;

import java.util.HashSet;
/**
 * 
 * @Title: Solution804_v1.java 
 * @Package leetcode.bst 
 * @Description: 804. 唯一摩尔斯密码词
 *                  使用HashSet求解问题
 *                  不借助额外的HashMap来保存26个字母和相应的摩尔斯密码的对应关系
 * @author CandyWall   
 * @date 2021年1月6日 下午2:22:38 
 * @version V1.0
 */
public class Solution804_v3_HashSet {
    public int uniqueMorseRepresentations(String[] words) {
        HashSet<String> set = new HashSet<>();
        String[] codes = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        for(String word: words) {
            StringBuilder sb = new StringBuilder();
            for(int i = 0; i < word.length(); i++) {
                // 获取每个单词的每个字母所对应的摩尔斯密码
                sb.append(codes[word.charAt(i) - 'a']);
            }
            set.add(sb.toString());
        }
        return set.size();
    }
    
    public static void main(String[] args) {
        String[] words = {"gin", "zen", "gig", "msg"};
        System.out.println(new Solution804_v3_HashSet().uniqueMorseRepresentations(words));
    }
}
